package com.gem.netty.service.card;

import com.gem.entity.Card;
import com.gem.entity.Game;
import com.gem.enums.ClientMessageType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.card.CardBaseFactory;
import com.gem.factory.game.GameFactory;
import com.gem.holder.ThreadLocalFactory;
import com.gem.netty.entity.CardInfo;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.utils.R;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author: Xhy
 * CreateTime: 2022-10-02 15:23
 * 校验获取卡牌格式
 */
public class GetCardVerifyService implements BusinessProcess<ClientContent> {
    @Override
    public void process(ProcessContext<ClientContent> context) {
        CardInfo cardInfo = context.getData().getContent().getCardInfo();

        // 是否为空
        if (cardInfo == null){
            context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("非法操作").type(ClientMessageType.GET_CARD.type));
            context.setNeedBreak(true);
            return;
        }
        Game game = GameFactory.get(context.getData().getRoomId());
        Map<Integer, Integer> userUseGems = cardInfo.getGems();

        Map<Integer, Integer> useGems = Card.getCardGems(cardInfo.getNo());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        TreeMap<Integer, List<Card>> userOwnCards = user.getCards();
        TreeMap<Integer, Integer> userOwnGem = user.getGems();

        // 玩家是否使用黄金替宝石
        Map<Integer, Integer> replaceGems = cardInfo.getReplaceGems();
        List<CardInfo> userTsCards = user.getTSCards();
        if (replaceGems.size() > 0){
            // 玩家使用黄金替宝石后，玩家需要选择一张牌丢弃
            CardInfo discardCardInfo = context.getData().getContent().getDiscardCardInfo();
            if (discardCardInfo!=null){
                boolean f = false;
                // 校验玩家是否丢弃的是压住牌库中的牌
                Iterator<CardInfo> cardInfoIterator = userTsCards.iterator();
                Integer no = null;
                while (cardInfoIterator.hasNext()){
                    CardInfo next = cardInfoIterator.next();
                    if (next.getNo() == discardCardInfo.getNo()){
                        no = next.getNo();
                        // 玩家丢弃卡牌
                        cardInfoIterator.remove();
                        f = true;
                        break;
                    }
                }

                if (!f){
                    context.setNeedBreak(true);
                    context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("只能选择自己牌库中的牌").type(ClientMessageType.GET_CARD.type));
                    return;
                }

                String roomId = context.getData().getRoomId();
                // 游戏牌库添加卡牌
                Card card = Card.getCard(no);
                CardBaseFactory.add(roomId,card);
            }
            boolean isTsCard = false;
            for (CardInfo userTsCard : userTsCards) {
                isTsCard = userTsCard.getNo() == cardInfo.getNo();
            }
            ThreadLocalFactory.put("isTsCard", isTsCard);
            // 玩家是否拥有足够的黄金
            AtomicInteger gemsNumber = new AtomicInteger(0);
            replaceGems.forEach((k,number)->{
                gemsNumber.getAndAdd(number);
            });
            int userGoldGem = user.getGems().get(GemType.GOLD.type);
            userGoldGem -= userTsCards.size(); // 计算出真实可听宝石的数量
            if (isTsCard) userGoldGem++;
            if (userGoldGem < gemsNumber.get()){
                context.setNeedBreak(true);
                context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("你没有足够的黄金").type(ClientMessageType.GET_CARD.type));
                return;
            }
            // 把替换的宝石添加至 gems
            replaceGems.forEach((k,number)->{
                if (userUseGems.containsKey(k)){
                    userUseGems.put(k,userUseGems.get(k)+number);
                }else {
                    userUseGems.put(k,number);
                }
            });

        }

//
//        // 校验卡牌所需要的宝石和当前用户使用的宝石
//        useGems.forEach((k,v)->{
//            if (userUseGems.get(k) - v < 0){
//                context.setNeedBreak(true);
//                context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("你没有这么多宝石").type(ClientMessageType.GET_CARD.type));
//                return;
//            }
//        });
//        if (context.getNeedBreak()) return;
//
//        // 校验卡牌所使用的buff是否超过玩家本身
//        if (cardInfo.getGemCards().size() > 0){
//            useGems.forEach((k,v)->{
//                // 用户使用自己没有的buff
//                if (!userOwnCards.containsKey(k)){
//                    context.setNeedBreak(true);
//                    context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("你尚未拥有该卡牌").type(ClientMessageType.GET_CARD.type));
//                    return;
//                }else if (userOwnCards.get(k).size() -v < 0 ){
//                    context.setNeedBreak(true);
//                    context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("你尚未拥有这么多卡牌").type(ClientMessageType.GET_CARD.type));
//                    return;
//                }
//            });
//        }
//
//        if (context.getNeedBreak()) return;


        // 换取的卡牌只能是场下的或者自己压住的牌库
        Integer cardNo = cardInfo.getNo();
        List<Card> cardList = game.getShowCards().get(cardInfo.getType());
        for (Card card : cardList) {
            if (card.getNo() == cardNo){
                return;
            }
        }
        for (CardInfo tsCard : userTsCards) {
            if (tsCard.getNo() == cardNo){
                return;
            }
        }
        context.setNeedBreak(true);
        context.setResult(R.error().data("color",MessageColorType.SYSTEM_MESSAGE.type).message("你不能换别人的牌哦").type(ClientMessageType.GET_CARD.type));
    }
}
